(function() {
    angular.module('app').component('attrProductComponent', {
        templateUrl: 'app/controllers/attr.controller/attr.product.component/attr.product.component.html',
        controller: attrProductComponent,
        controllerAs: 'vm'
    });

    attrProductComponent.$inject = ['attrProductComponentService', 'productBasicService', '$rootScope', '$scope', '$loading', 'util'];

    function attrProductComponent(attrProductComponentService, productBasicService, $rootScope, $scope, $loading, util) {
        var vm = this;


        // console.log('-------------attrProductComponent.js loading-------------');

        vm.seen = false;

        vm.table = {
            count: 0,
            products: [],
            totals: []
        }

        vm.PageNumber = 1;
        vm.sortColumn = "sellMoney";
        vm.maxMoney = 0;

        vm.option = {

            xAxis: {
                type: 'value',
                name: '销量',
                nameTextStyle: {
                    color: '#6e6e6e',
                    fontSize: 14
                },
                splitLine: {
                    lineStyle: {
                        type: 'dashed'
                    }
                },
                offset: 10,
                axisLabel: {
                    formatter: function(value, index) {
                        var len = String(value).length;

                        if (value < 10000) {
                            return value;
                        }

                        switch (len) {
                            case 5:
                                return (value / 10000).toFixed(2) + '万';
                            case 6:
                                return (value / 10000).toFixed(2) + '万';
                            case 7:
                                return (value / 10000).toFixed(2) + '万';
                            case 8:
                                return (value / 10000).toFixed(2) + '万';
                            case 9:
                                return (value / 100000000).toFixed(2) + '亿';
                            case 10:
                                return (value / 100000000).toFixed(2) + '亿';
                            case 11:
                                return (value / 100000000).toFixed(2) + '亿';
                            case 12:
                                return (value / 100000000).toFixed(2) + '亿';
                        }

                        return value;
                    }
                }
            },
            grid: {
                left:'60'
            },
            yAxis: {
                type: 'value',
                name: '价格',
                nameLocation: 'end',
                nameGap: 20,
                splitLine: {
                    lineStyle: {
                        type: 'dashed'
                    }
                }
            },
            tooltip: {
                padding: 10,
                backgroundColor: '#222',
                borderColor: '#777',
                borderWidth: 1,
                formatter: function(obj) {
                    var value = obj.data;
                    // console.log(value);
                    if (value.constructor == Array) {
                        return '<div style="border-bottom: 1px solid rgba(255,255,255,.3); font-size: 16px;padding-bottom: 7px;margin-bottom: 7px"><span>第' + (Number(value[6]) + 1) + "位:</span>" + '<span style="max-width:140px;vertical-align:bottom;display:inline-block;overflow:hidden;text-overflow: ellipsis;">' + value[2] + '</span></div>' + "销量" + '：' + util.bigNumFormat(value[0]) + '<br>' + "价格" + '：' + value[1] + '元<br>' + "销售额" + '：' + util.bigNumFormat(value[4]) + '<br>';
                    } else {
                        return value.name + ' :<br/>' + value.value;
                    }

                    // console.log(value.constructor);


                }
            },
            series: [{
                name: '宝贝',
                data: [],
                type: 'scatter',
                symbolSize: function(data) {
                    // console.log(data);
                    var index = (300 / (Number(data[6]) + 1));
                    // var index = (1000-300)/(100-20)*(300-Number(data[6])-20)+300;
                    return Math.sqrt(index * 14);
                },
                itemStyle: {
                    normal: {
                        color: function() {
                            return (function(m, s, c) {
                                    return (c ? arguments.callee(m, s, c - 1) : '#') +
                                        s[m.floor(m.random() * 16)]
                                })(Math, '0123456789abcdef', 5)
                                /*return "#" + ("00000" + ((Math.random() * 16777215 + 0.5) >> 0).toString(16)).slice(-6);*/
                        },
                        shadowBlur: 10,
                        shadowColor: 'rgba(120, 36, 50, 0.5)',
                        shadowOffsetY: 5

                    }
                },
                markArea: {
                    silent: true,
                    itemStyle: {
                        normal: {
                            color: 'transparent',
                            borderWidth: 1,
                            borderType: 'dashed'
                        }
                    }/*,
                    data: [
                        [{
                            name: '分布区间',
                            xAxis: 'min',
                            yAxis: 'min'
                        }, {
                            xAxis: 'max',
                            yAxis: 'max'
                        }]
                    ]*/
                },
                markPoint: {
                    data: [
                        { type: 'max', name: '最大值' },
                        { type: 'min', name: '最小值' }
                    ]
                },
                markLine: {
                    lineStyle: {
                        normal: {
                            type: 'solid'
                        }
                    },
                    data: [
                        { type: 'average', name: '平均值' }
                    ]
                }

            }]
        };


        vm.pageChanged = pageChanged;

        //品牌详情
        vm.getBrandBasic = productBasicService.getBrandBasic; //function

        //店铺详情
        vm.getShopBasic = productBasicService.getShopBasic; //function

        //获取日监控
        vm.getMonitorData = productBasicService.getMonitorData; //function


        //sort
        vm.sort = sort; //function


        var clearFun = $rootScope.$on('emit_attr_product_component_EV', function(e, filter, selected_props, type) {
            vm.json = {};
            vm.json = {
                category: filter.category,
                property: selected_props,
                shopType: filter.shopType,
                type: type
            };
            _init(vm.json);
        });

        var hideEV = $rootScope.$on('pool_emit_hide_EV',function(){

            vm.seen = false;

        });

        $scope.$on('$destroy', function() {
            clearFun();
            hideEV();
        });



        function _init(json) {


            if (!vm.seen) {
                vm.seen = true;
            }

            $loading.start('attr_product');

            $rootScope.$emit('echart_draw_line_loading_EV','draw_attr_prod');

            attrProductComponentService.get(json).then(function(data) {

                if (data.length == 0) {
                    vm.seen = false;
                    vm.table.count = 0;
                    
                } else {
                    vm.table.count = data.length / 11 * 10;
                    vm.table.totals = data;

                    var dataOptions = [];
                    angular.forEach(vm.table.totals, function(prod, index) {
                        prod.index = index + 1;
                        dataOptions.push([prod.sellAmount, prod.price, prod.title, prod.uid, prod.sellMoney, prod.picUrl, index]);
                    });
                    pageChanged(1);
                    chartInit(dataOptions);
                }

                productBasicService.shopBasicInit(); //店铺详情的序号
                productBasicService.brandBasicInit(); //店铺详情的序号
                productBasicService.monitorDataInit(); //日监控所在的序号
                $loading.finish('attr_product');
            }, function(err) {
                $loading.finish('attr_product');
            });
        }

        function chartInit(data) {


            var option = angular.copy(vm.option);

            option.series[0].data = data;


            $rootScope.$emit('echart_draw_line_EV', option, 'draw_attr_prod');
        }

        // 页面切换
        function pageChanged(pageNumber) {

            vm.PageNumber = pageNumber;
            vm.table.products = vm.table.totals.slice((pageNumber - 1) * 10, (pageNumber - 1) * 10 + 10);

        }

        function sort(sortColumn) {
            vm.sortColumn = sortColumn
            vm.table.totals.sort(function(a, b) {
                return b[sortColumn] - a[sortColumn];
            });

            pageChanged(vm.PageNumber);
        }
    }
})();
